package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ISAreaService;
import com.ruoyi.system.service.ISSubstationDeviceService;
import com.spire.doc.*;
import com.spire.doc.collections.SectionCollection;
import com.spire.doc.collections.TableCollection;
import com.spire.doc.documents.Paragraph;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SSubstationMapper;
import com.ruoyi.system.service.ISSubstationService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 变电站信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class SSubstationServiceImpl extends ServiceImpl<SSubstationMapper, SSubstation> implements ISSubstationService
{

    @Autowired
    private SSubstationMapper sSubstationMapper;

    @Autowired
    private ISAreaService areaService;

    @Autowired
    private ISSubstationDeviceService substationDeviceService;

    @Override
    public String importData(MultipartFile file) throws Exception {

        ExcelUtil<SSubstationDevice> util = new ExcelUtil<SSubstationDevice>(SSubstationDevice.class);
        Sheet sheet = util.getSheet("", file.getInputStream());
        int lastRowNum = sheet.getLastRowNum();
        if(lastRowNum <= 0) return "数据为空！";

        // 创建变电站设备对象，用来保存表格中的前4行的值
        SSubstationDevice substationDevice = substationDeviceService.createSubstationDevice(sheet);
        // 设置设备电压等级
        if(StringUtils.isNotBlank(substationDevice.getSubstationName())) {
            String substationName = substationDevice.getSubstationName();
            if(StringUtils.isNotBlank(substationName)) {
                long count = this.count(new LambdaQueryWrapper<SSubstation>()
                        .eq(SSubstation::getSubstationName, substationName));
                if (count > 0) {
                    return "厂站已存在！";
                }

                SSubstation sSubstation = new SSubstation();
                sSubstation.setCreateBy(SecurityUtils.getUserId().toString());
                sSubstation.setCreateTime(new Date());
                sSubstation.setSubstationName(substationName);
                this.save(sSubstation);
                return "导入成功！";
            }
        }
        return "导入成功！";
    }

    @Override
    public SSubstation getOrCreateByName(String name) {
        if(StringUtils.isBlank(name)) return new SSubstation();
        SSubstation sSubstation = this.getOne(new LambdaQueryWrapper<SSubstation>()
                .eq(SSubstation::getSubstationName, name));
        if (StringUtils.isNull(sSubstation)) {
            sSubstation = new SSubstation();
            sSubstation.setCreateBy(SecurityUtils.getUserId().toString());
            sSubstation.setCreateTime(new Date());
            sSubstation.setSubstationName(name);
            this.save(sSubstation);
        }
        return sSubstation;
    }

    @Override
    public List<SSubstation> getList(SSubstation sSubstation) {

        String leaderId = SecurityUtils.getLeaderId();
        QueryWrapper<SSubstation> queryWrapper = sSubstation.queryWrapper();
        if(StringUtils.isBlank(sSubstation.getAreaId())) {
            // 如果是班组长或者组员只能查看绑定的地区下的变电站
            if (StringUtils.isNotBlank(leaderId)) {
                List<SArea> sAreas = areaService.list(new LambdaQueryWrapper<SArea>().eq(SArea::getUserId, leaderId));
                if (sAreas.size() > 0) {
                    List<String> areaIds = sAreas.stream().map(SArea::getId).collect(Collectors.toList());
                    queryWrapper.in("s.area_id", areaIds);
                }
            }
        }
        return sSubstationMapper.getList(queryWrapper);
    }

    @Transactional
    @Override
    public boolean updateSubstationById(SSubstation sSubstation) {
        List<SSubstationDevice> list = substationDeviceService.list(new LambdaQueryWrapper<SSubstationDevice>()
                .eq(SSubstationDevice::getSubstationId, sSubstation.getId()));

        for (SSubstationDevice substationDevice : list) {
            substationDeviceService.createQRCode(substationDevice.getId());
        }

        return this.updateById(sSubstation);
    }
}
